package com.adb.androidwebalbum.uc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.adb.androidwebalbum.utils.Log;

public class CustomTopMenu implements OnClickListener{
	
	private String TAG = CustomTopMenu.class.getCanonicalName();
	
	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	private Button btn_back;
	private TextView textView_topText;
	private Activity mActivity;
	
	OnHeadNavigationSelectedListener mCallBack;
	
	public boolean isShowing() { return mIsShowing; }
	
	public CustomTopMenu(Context context, LayoutInflater lo) {
		mContext = context;
		mLayoutInflater = lo;
		mCallBack = (OnHeadNavigationSelectedListener)context;
		mActivity = (Activity)context;
	}
	
	
	@SuppressWarnings("deprecation")
	public synchronized void show(View v,int mPosition,ArrayList<ImageBean> arrListImageBean) {
		
		Log.debug(TAG,"[ CustomTopMenu ] ");
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		View mView= mLayoutInflater.inflate(R.layout.custom_top_menu, null);

		textView_topText = (TextView)mView.findViewById(R.id.textView_topText);
		setPageSize(mPosition, arrListImageBean);
		
		btn_back = (Button)mView.findViewById(R.id.btn_back);
		btn_back.setOnClickListener(this);
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.MATCH_PARENT,65, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
    	mPopupWindow.setWidth(display.getWidth());
    	mPopupWindow.showAtLocation(v, Gravity.TOP, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}

	public void onClick(View v) {
		mCallBack.onButtonSelected(v.getId());
	}
	
	public void setPageSize(final int mPosition,final ArrayList<ImageBean> arrListImageBean){
		
		if(isShowing()){
			Log.println("[ TOP NUMBER ] " + mPosition);
			mActivity.runOnUiThread(new Runnable() {
				
				public void run() {
					
					textView_topText.setText(new StringBuilder().append((mPosition+1)).append(" of ").append(arrListImageBean.size()));
				}
			});
		}
		
		
	}
}
